package com.ruoyi.common.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @ProjectName: ruoyi
 * @ClassName: SMSUtils
 * @Description: 短信服务
 * @Author: @Abeiv
 * @Date: 2020-3-2 15:08
 */
public class SMSUtils {
    private static final String ACCESS_KEY_ID = "LTAI4FqccRYfxS3tBKqGgwXL";
    private static final String ACCESS_KEY_SECRET = "BGXcVEHSiPVYjZG77IkisiZkqxJwA7";
    private static final String TEMPLATE_CODE = "SMS_184631675"; //模板代码
    private static final String REGION_ID = "cn-hangzhou";
    private static final String ALI_VERSION = "2017-05-25";
    private static final String ALI_DOMAIN = "dysmsapi.aliyuncs.com";
    private static final String SING_NAME = "北视网络";

    public static CommonResponse SMS(String action, String phone, String code,MethodType method){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(method);
        request.setDomain(ALI_DOMAIN);
        request.setVersion(ALI_VERSION);
        request.setAction(action);
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SING_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response);
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new CommonResponse();
    }


    // 发送短信
    public static CommonResponse sendSMS(String phone,String code){
        return SMS("SendSms",phone,code,MethodType.POST);
    }

}
